package com.up.upzl.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.up.upzl.base.service.impl.BaseServiceImpl;
import com.up.upzl.biz.domain.ArticleCategory;
import com.up.upzl.biz.domain.ArticleCategory.Type;
import com.up.upzl.biz.repository.ArticleCategoryRepository;
import com.up.upzl.biz.service.ArticleCategoryService;


@Service
public class ArticleCategoryServiceImpl extends BaseServiceImpl<ArticleCategory, Long> implements ArticleCategoryService{

	@Autowired
	private ArticleCategoryRepository ArticleCategoryRepository;
	
	
	/*
	 * 根据父级id分类名称查询分类
	 * 
	 */
	@Override
	public Page<ArticleCategory> findByCondition(Long parentId, String name, Pageable page) {
		return ArticleCategoryRepository.findAll(new Specification<ArticleCategory>() {

			@Override
			public Predicate toPredicate(Root<ArticleCategory> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (!StringUtils.isEmpty(name)) {
					predicates.add(cb.and(cb.like(root.get("name"), "%" + name + "%")));
				}
				if (!StringUtils.isEmpty(parentId)) {
					predicates.add(cb.and(cb.equal(root.get("parentArticleCategory"), parentId)));
				}else {
					predicates.add(cb.and(cb.isNull(root.get("parentArticleCategory"))));
				}
				
				
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		}, page);
	}
	
	@Override
	public List<ArticleCategory> findByTypeX(Type type, Boolean publish, Boolean mold) {
		//TODO 根据类型查询分类信息
		return ArticleCategoryRepository.findAll(new Specification<ArticleCategory>() {
			@Override
			public Predicate toPredicate(Root<ArticleCategory> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (!StringUtils.isEmpty(type)) {
					predicates.add(cb.and(cb.equal(root.get("type"), type)));
				}
				if (!StringUtils.isEmpty(publish)) {
					predicates.add(cb.and(cb.equal(root.get("publish"), publish)));
				}
				if (mold) {
					predicates.add(cb.and(cb.isNull(root.get("parentArticleCategory"))));
				}else {
					predicates.add(cb.and(cb.isNotNull(root.get("parentArticleCategory"))));
				}
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		},new Sort(Direction.DESC, "sort"));
	}

	@Override
	public List<ArticleCategory> findByParentId(Long parentId) {
		// TODO 根据父级编号查询分类信息
		return ArticleCategoryRepository.findAll(new Specification<ArticleCategory>() {
			@Override
			public Predicate toPredicate(Root<ArticleCategory> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (!StringUtils.isEmpty(parentId)) {
					predicates.add(cb.and(cb.equal(root.get("parentArticleCategory"), parentId)));
				}else {
					predicates.add(cb.and(cb.isNull(root.get("parentArticleCategory"))));
				}
				
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		},new Sort(Direction.DESC, "sort"));
	}
	
}
